/**=========================================================
 * Module: login.js
 * login api
 =========================================================*/

App.controller('LoginController', ['$scope', '$rootScope', '$translate', '$state', '$http', 'Utils',
    function($scope, $rootScope, $translate, $state, $http, utils) {
        // bind here all data from the form
        $scope.account = {};
        // place the message if something goes wrong
        $scope.authMsg = '';
        $scope.needCaptha = false;

        $scope.login = function() {
            $scope.authMsg = '';

            if($scope.loginForm.$valid) {
                $http({
                    url: utils.getBackendPath('/login'),
                    method: 'POST',
					withCredentials: true, // 跨域
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function(obj) {
                        var str = [];
                        for(var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        return str.join("&");
                    },
                    data: {
                        'username': $scope.account.username, 
                        'password': $scope.account.password,
                        'remember-me': $scope.account.remember,
                        'captcha': $scope.account.captcha
                    }
                }).then(function(response){
                    var result = response.data;
                    if (result.success == "true") {
                        $rootScope.app.isSessionValid = true;
                        $rootScope.app.user = {
                            code: result.code,
                            name: localStorage.NG_TRANSLATE_LANG_KEY == 'zh' ? result.cnname : result.enname,
                            zhname: result.cnname,
                            enname: result.enname,
                            faceUrl: utils.isEmpty(result.faceUrl) ? 'app/img/face.jpg' : result.faceUrl
                        }
                        $state.go('app.loading');
                    }else{
                        $http({
                            method:'GET',
                            url: utils.getBackendPath('/sysUtil/needCaptcha'),
                            params:{
                                username: $scope.account.username
                            }
                        }).then(
                            function(response){
                                var result = response.data;
                                if (utils.isEmpty(result) || result.needCaptcha == 'true'){
                                    $scope.needCaptha = true;
                                }
                            }
                        );

                        if(result.msg == "ExpiredCaptcha")
                            $scope.authMsg = $translate.instant("login.EXPIRED_CAPTCHA");
                        else if(result.msg == "IncorrectCaptcha")
                            $scope.authMsg = $translate.instant("login.INCORRECT_CAPTCHA");
                        else 
                            $scope.authMsg = $translate.instant("login.INCORRECT_CREDENTIALS");
                    }
                }).catch(function(x) {
                    $scope.authMsg = 'Server Request Error';
                });
            }
           
        };
    }
]);
